package game.Sprites;

import game.Vector3;
import javafx.scene.SnapshotParameters;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import javafx.scene.paint.Color;
import java.awt.geom.Rectangle2D;

public class DebrySprite extends Sprite {
    protected double speed = 2.0;
    protected double size;
    protected float opacity;
    protected Image img;

    public DebrySprite(Image img, Vector3 position, double rotation, double size) throws Exception {
        super(position, rotation, Color.RED);
        this.position.z = 1.0;

        this.opacity = 1.f;
        this.size = size;
        this.img = img;
    }

    @Override
    public void draw(GraphicsContext g2D, Vector3 screenPos, double squareRes) {

        //Apply rotation
        ///ImageView iv = new ImageView(img);
        //iv.setRotate(Math.toDegrees(rotation));
        //SnapshotParameters params = new SnapshotParameters();
       // params.setFill(javafx.scene.paint.Color.TRANSPARENT);
        //Image rotatedImage = iv.snapshot(params, null);

        g2D.setGlobalAlpha(opacity);
        // Draw image
        g2D.drawImage(img,
                (int) screenPos.x - (int) (squareRes * size / 2),(int) screenPos.y - (int) (squareRes * size / 2),
                (int) (squareRes * size), (int) (squareRes * size));
        g2D.setGlobalAlpha(1.0);
    }

    @Override
    public Rectangle2D.Double getBound() {
        return new Rectangle2D.Double(this.position.x - size/2, this.position.y - size/2, size, size);
    }

    @Override
    public void update(double deltaTime) {
        this.position = this.position.add(
                new Vector3(Math.cos(rotation), Math.sin(rotation), 0.0).multiply(speed).multiply(deltaTime)
        );

        this.opacity -= deltaTime * 1.0;
        if(this.opacity <= 0.0){
            this.destroy();
        }

        this.size += deltaTime * this.speed * 0.5;

        this.speed = this.speed * 0.98;
    }
}
